package demo.pattern;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : GuoFei
 * @since : 2019/6/25 17:34
 */
public class TestMain {
    public static void main(String[] args) {
        System.out.println("begin");
        run();
        System.out.println("end");
    }

    public static void run(){
//        ExecutorService executors = Executors.newFixedThreadPool(100);
//        ExecutorService executors = Executors.newCachedThreadPool();
//        ExecutorService executors = Executors.newScheduledThreadPool(100);
        ExecutorService executors = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            executors.submit(() -> item(finalI));
        }
        executors.shutdown();
        while (!executors.isTerminated()) {}
    }

    public static void item(int i){
        System.out.println(Thread.currentThread().getName()+"-----run"+"\t"+"task【"+i+"】");
        sleep(10000);
        System.out.println(Thread.currentThread().getName()+"-----finish"+"\t"+"task【"+i+"】");
    }

    public static void sleep(int second){
        try {
            Thread.sleep(second);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
